package com.liang.lollipop.lreader.base;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.liang.lollipop.lreader.util.LItemTouchCallback;
import com.liang.lollipop.lreader.util.LToast;
import com.liang.lollipop.lreader.util.OnScrollDownListener;

/**
 * Created by Lollipop on 2017/09/08.
 * 基础的Activity
 */
public class BaseActivity extends AppCompatActivity
        implements View.OnClickListener,
        LItemTouchCallback.OnItemTouchCallbackListener,
        OnScrollDownListener.OnScrollListener{

    private boolean isShowBack = true;

    protected Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            onHandler(msg);
        }
    };

    protected void onHandler(Message message){

    }

    @Override
    protected void onStart() {
        super.onStart();
        if(isShowBack&&getSupportActionBar()!=null){
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    protected void T(String str){
        LToast.makeText(this,str, Toast.LENGTH_SHORT).show();
    }

    protected void setToolBar(int id){
        Toolbar toolbar = (Toolbar) findViewById(id);
        if(toolbar==null)
            return;
        setSupportActionBar(toolbar);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==android.R.id.home){
            super.onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    protected void setShowBack(boolean showBack) {
        isShowBack = showBack;
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onSwiped(int adapterPosition) {

    }

    @Override
    public boolean onMove(int srcPosition, int targetPosition) {
        return false;
    }

    @Override
    public void onItemViewClick(RecyclerView.ViewHolder holder, View v) {

    }

    @Override
    public void onScroll(boolean down, int newState) {

    }

    @Override
    public void onMore() {

    }

    protected void scrollWithRecyclerView(RecyclerView recyclerView){
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if(layoutManager instanceof LinearLayoutManager){
            LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
            recyclerView.addOnScrollListener(new OnScrollDownListener(linearLayoutManager,this));
        }else {
            throw new RuntimeException("scrollWithRecyclerView() ---> RecyclerView.LayoutManager mast be LinearLayoutManager");
        }
    }

}
